/*
 * Copyright (C) 2003, Velocityme Partnership
 * SystemPropertyBean.java
 *
 * Created on 24 September 2004, 08:27
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="SystemProperty"
 *           jndi-name="ejb/SystemProperty"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="systemproperty"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findAll()"
 *              query="SELECT OBJECT(o) FROM SystemProperty o"
 *
 * @ejb.finder signature="com.velocityme.interfaces.SystemProperty findByName(java.lang.String p_name)"
 *              query="SELECT OBJECT(o) FROM SystemProperty o WHERE o.name = ?1"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class SystemPropertyBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
      
    /** @ejb.create-method */
    public SystemPropertyPK ejbCreate(com.velocityme.valueobjects.SystemPropertyValue p_value) throws CreateException {
        try {
            setSystemPropertyId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new CreateException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.valueobjects.SystemPropertyValue p_value) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setSystemPropertyValue(p_value);
    }
    
    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("systemproperty","systemPropertyId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public SystemPropertyLocal getLocal() {
        return (SystemPropertyLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.SystemPropertyValue getSystemPropertyValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setSystemPropertyValue(com.velocityme.valueobjects.SystemPropertyValue p_value);
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="systemPropertyId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getSystemPropertyId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setSystemPropertyId(java.lang.Integer systemPropertyId);

    /**
     * @ejb.persistence column-name="name"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getName();
    public abstract void setName(java.lang.String name);
    
    /**
     * @ejb.persistence column-name="value"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getValue();
    public abstract void setValue(java.lang.String value);
    
   /**
     * @ejb.persistence column-name="isPrivate"
     * @ejb.interface-method view-type="local"
     */
    public abstract boolean getIsPrivate();
    /** @ejb.interface-method view-type="local" */
    public abstract void setIsPrivate(boolean isPrivate);
    
    /**
     * @ejb.persistence column-name="isEditable"
     * @ejb.interface-method view-type="local"
     */
    public abstract boolean getIsEditable();
    /** @ejb.interface-method view-type="local" */
    public abstract void setIsEditable(boolean isEditable);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
